package com.itbaizhan.springsecuritydemo.handler;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyLoginFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        System.out.println("记录失败日志。。。");
        response.sendRedirect("/fail"); //重定向，跳转到另一页面
    }
}
/*想可心跳转访问要在securityConfig.java配置
http.formLogin() // 使用表单登录
   .loginPage("/login.html") // 自定义登录页面
*  .failureHandler(new MyLoginFailureHandler()); //登录失败处理器
*
* // 需要认证的资源
http.authorizeRequests()
   .antMatchers("/login.html").permitAll() // 登录页不需要认证
   .antMatchers("/fail").permitAll() // 失败页不需要认证
   .anyRequest().authenticated(); //其余所有请求都需要认证*/